package com.test.schoolhelper.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseFragment;
import com.test.schoolhelper.databinding.FragmentNewsBinding;
import com.test.schoolhelper.entity.News;
import com.test.schoolhelper.ui.activity.NewsActivity;
import com.test.schoolhelper.ui.adapter.NewSAdapter;
import com.test.schoolhelper.utils.Constants;
import com.test.schoolhelper.utils.LogUtils;
import com.test.schoolhelper.utils.OkHttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

// TODO: 2024/5/29 新闻
public class NewsFragment extends BaseFragment<FragmentNewsBinding> {

    @Override
    protected void initData() {
        super.initData();
       sendRequest("top");
    }

    private void sendRequest(String type) {

        OkHttpUtil.sendHttpRequest(Constants.JUHE_NEWS_URL + "?type=" + type + "&key=" + Constants.JUHE_NEWS_KEY, new Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();

                    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                    News news = gson.fromJson(responseBody, News.class);

                    if (news.getReason().contains("超过每日可允许请求次数!")) {
                        handler.sendEmptyMessage(0);
                        return;
                    }
                    if (news.getResult() != null) {
                        newList.clear();
                        newList.addAll(news.getResult().getData());
                        handler.sendEmptyMessage(1);
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                LogUtils.showLog(e.getMessage());
            }
        });
    }


    List<News.Result.Data> newList = new ArrayList<>();
    private NewSAdapter newSAdapter;

    private void initAdapter() {

        if (newSAdapter==null){
            newSAdapter = new NewSAdapter(getActivity(), newList);
            getBinding().rvData.setLayoutManager(new LinearLayoutManager(getActivity(),
                    LinearLayoutManager.VERTICAL, false));
            getBinding().rvData.setAdapter(newSAdapter);
        }else {
            newSAdapter.setList(newList);
            newSAdapter.notifyDataSetChanged();
        }

        newSAdapter.setListener(pos -> {
            Intent intent = new Intent(getContext(), NewsActivity.class);
            intent.putExtra("data", newList.get(pos));
            startActivity(intent);
        });
    }

    Handler handler = new Handler(msg -> {
        switch (msg.what) {
            case 0:
                Toast.makeText(getActivity(), "超过每日可允许请求次数!", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                initAdapter();
                break;
        }
        return false;
    });

    @Override
    protected int initLayout() {
        return R.layout.fragment_news;
    }

    @Override
    protected FragmentNewsBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        return FragmentNewsBinding.inflate(inflater);
    }
}
